package models

import (
	"github.com/ylywyn/jpush-api-go-client"
	// "fmt"
	"github.com/astaxie/beego"
)

const (
	appKey = "c876fd8e4fadaf14e1254a6f"
	secret = "39ef4270635fe30f26f058e5"
)

func JPush(message string, audience []string, extra map[string]interface{}) error {

	//Platform
	//构建要推送的平台： jpushclient.Platform
	var pf jpushclient.Platform
	var option jpushclient.Option
	// pf.Add(jpushclient.ANDROID)
	// pf.Add(jpushclient.IOS)
	// pf.Add(jpushclient.WINPHONE)
	pf.All()

	option.SetApns(true)
	//Audience
	//构建接收听众： jpushclient.Audience

	//s := []string{"100067"}
	//ad.SetTag(s)
	beego.Debug(len(audience))
	var ad jpushclient.Audience
	if audience[0] != "" {
		s := audience
		ad.SetAlias(s)
	} else {
		ad.All()
	}
	//ad.SetID(s)
	//ad.All()

	//Notice
	//构建通知 jpushclient.Notice，或者消息： jpushclient.Message
	var notice jpushclient.Notice
	notice.SetAlert("alert_test")
	notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: message, Extras: extra})
	notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: message, Extras: extra})
	notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: message, Extras: extra})

	// var msg jpushclient.Message
	// msg.Title = title
	// msg.Content = message

	//构建jpushclient.PayLoad
	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetNotice(&notice)
	// payload.SetMessage(&msg)

	bytes, _ := payload.ToBytes()
	beego.Debug(string(bytes))

	//push
	//构建PushClient，发出推送
	c := jpushclient.NewPushClient(secret, appKey)
	str, err := c.Send(bytes)
	if err != nil {
		beego.Debug("err:", err.Error())
	} else {
		beego.Debug("ok:", str)
	}
	payload.SetOptions(&option)
	return err
}

//发送推送用
// uid:=info[0]["fid"].(string)
// 	extra:=make(map[string]interface{})
// 	extra["jump_type"]="2"//订单详情
// 	extra["id"]=oid
// 	extra["uid"]=uid
// 	err:=models.JPush("您有一笔订单需发货。",[]string{uid},extra)
// 	if err!=nil{
// 		js["status"]=0
// 	}else {
// 		js["status"]=1
// 	}
